//
//  TYZCoreInfo.m
//  TYZ_SC
//
//  Created by 李贤立 on 2017/12/12.
//  Copyright © 2017年 Capfer. All rights reserved.
//

#import "TYZCoreInfo.h"
#import "ESTFMDBTool.h"
#import "TYZLanguageTool.h"

@implementation TYZCoreInfo
TYZSingleto_m(CoreInfo)
/**
 *  初始化基本信息
 */
+ (void)initial {
    // 计算屏幕宽高
    BaseInfo.screenWidth = [UIScreen mainScreen].bounds.size.width;
    BaseInfo.screenHeight = [UIScreen mainScreen].bounds.size.height;
    // 适配宽高和字体的倍数
    BaseInfo.widthSuited = BaseInfo.screenWidth / 375;
    BaseInfo.heightSuited = BaseInfo.screenHeight / 667;
    BaseInfo.fontSuited = hypot(BaseInfo.screenWidth, BaseInfo.screenHeight) / hypot(375, 667);
    // 获取版本号
    NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
    CoreInfo.app_version = infoDictionary[@"CFBundleShortVersionString"];
    // 判断设备类型
    CoreInfo.DeviceType = [TYZCoreInfo getDeviceType];
    // 获取用户ID
    CoreInfo.user_id = [[NSUserDefaults standardUserDefaults] objectForKey:Symbol_User_Id];
    // 设置语言和字体
    [TYZLanguageTool configLanguageAndFonSize];
    // 设置默认主题
    CoreInfo.scrmType = SCRM_TYPE_ASSISTANT;
    // 获取登录状态
    CoreInfo.isLogined = [[NSUserDefaults standardUserDefaults] boolForKey:Symbol_LoginState];
    CoreInfo.userName = [[NSUserDefaults standardUserDefaults] objectForKey:Symbol_User_Name];
    CoreInfo.password = [[NSUserDefaults standardUserDefaults] objectForKey:Symbol_User_Password];
    CoreInfo.deviceToken = [[NSUserDefaults standardUserDefaults] objectForKey:Symbol_Device_Token];
    // 初始化网络连接
    [TYZNetworkingTool initial];
    // 初始化数据库
    [ESTFMDBTool sharedFMDBTool];
    // 设置通知数量
    CoreInfo.notifacationNumber = 0;
    // 设置APP_ID
    BaseInfo.app_id = APP_ID_1;
    // 设置版本检测的URL
    NetTool.url = [NSString stringWithFormat:@"%@%@%@%@%@", Network_Address, @":", Port_Number, @"/", Router_Name];
}
+ (TYZDeviceType)getDeviceType {
    switch ([UIDevice currentDevice].userInterfaceIdiom) {
        case UIUserInterfaceIdiomPhone:
            return DEVICE_TYPE_IPHONE;
            break;
        case UIUserInterfaceIdiomPad:
            return DEVICE_TYPE_IPAD;
            break;
        case UIUserInterfaceIdiomCarPlay:
            return DEVICE_TYPE_CARPLAY;
            break;
        case UIUserInterfaceIdiomUnspecified:
            return DEVICE_TYPE_OTHER;
            break;
        default:
            return DEVICE_TYPE_OTHER;
            break;
    }
}
/**
 *  设置程序图标显示的数目
 */
+ (void)setAppBadgeNumber:(NSInteger)AppBadgeNumber {
    // 进行注册
    UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert categories:nil];
    [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
    [UIApplication sharedApplication].applicationIconBadgeNumber = AppBadgeNumber;
}
- (void)setNotifacationNumber:(NSInteger)notifacationNumber {
    _notifacationNumber = notifacationNumber;
    [TYZCoreInfo setAppBadgeNumber:notifacationNumber];
}
- (ESTUserInfo *)userInfo {
    if (_userInfo == nil) {
        _userInfo = [[ESTUserInfo alloc] init];
    }
    return _userInfo;
}
- (void)setAuth_token:(NSString *)auth_token {
    _auth_token = auth_token;
    [[NSUserDefaults standardUserDefaults] setObject:auth_token forKey:Symbol_Auth_Token];
}
- (void)setUser_id:(NSString *)user_id {
    _user_id = user_id;
    [[NSUserDefaults standardUserDefaults] setObject:user_id forKey:Symbol_User_Id];
}
- (void)setRememberMe:(NSString *)rememberMe {
    _rememberMe = rememberMe;
    [[NSUserDefaults standardUserDefaults] setObject:rememberMe forKey:Symbol_RememberMe];
}
- (void)setIsLogined:(BOOL)isLogined {
    _isLogined = isLogined;
    [[NSUserDefaults standardUserDefaults] setBool:isLogined forKey:Symbol_LoginState];
}
- (void)setDeviceToken:(NSString *)deviceToken {
    _deviceToken = deviceToken;
    [[NSUserDefaults standardUserDefaults] setObject:deviceToken forKey:Symbol_Device_Token];
}
- (void)setUserName:(NSString *)userName {
    _userName = userName;
    [[NSUserDefaults standardUserDefaults] setObject:userName forKey:Symbol_User_Name];
}
- (void)setPassword:(NSString *)password {
    _password = password;
    [[NSUserDefaults standardUserDefaults] setObject:password forKey:Symbol_User_Password];
}
- (void)setOpenNotification:(BOOL)openNotification {
    _openNotification = openNotification;
    [[NSUserDefaults standardUserDefaults] setBool:openNotification forKey:Symbol_Notification_State];
}
- (void)setScrmType:(ESTSCRMAPPTYPE)scrmType {
    _scrmType = scrmType;
    switch (scrmType) {
        case SCRM_TYPE_ASSISTANT:
            BaseInfo.themeColor = TYZColor(0x2E, 0xAB, 0xFB);
            BaseInfo.themeGrayColor = TYZColor(0x6a, 0x72, 0x7a);
            BaseInfo.navigationImage = [UIImage imageWithColor:BaseInfo.themeColor];
            BaseInfo.globalBackgroundColor = ESTBackgroundColor;
            BaseInfo.backButtonImageNameNormal = Image_Navigation_Back_Normal_White;
            BaseInfo.backButtonImageNameHighlight = Image_Navigation_Back_Highlight_White;
            break;
        case SCRM_TYPE_MANAGER:
            BaseInfo.themeColor = TYZColor(0x47, 0x6E, 0xB3);
            BaseInfo.themeGrayColor = TYZColor(0x6a, 0x72, 0x7a);
            BaseInfo.navigationImage = [UIImage imageWithColor:BaseInfo.themeColor];
            BaseInfo.globalBackgroundColor = ESTBackgroundColor;
            BaseInfo.backButtonImageNameNormal = Image_Navigation_Back_Normal_White;
            BaseInfo.backButtonImageNameHighlight = Image_Navigation_Back_Highlight_White;
            break;
        case SCRM_TYPE_BOSS:
            BaseInfo.themeColor = TYZColor(0x47, 0x6E, 0xB3);
            BaseInfo.themeGrayColor = TYZColor(0x6a, 0x72, 0x7a);
            BaseInfo.navigationImage = [UIImage imageWithColor:BaseInfo.themeColor];
            BaseInfo.globalBackgroundColor = ESTBackgroundColor;
            BaseInfo.backButtonImageNameNormal = Image_Navigation_Back_Normal_White;
            BaseInfo.backButtonImageNameHighlight = Image_Navigation_Back_Highlight_White;
            break;
        case SCRM_TYPE_ACTIVITY:
            BaseInfo.themeColor = TYZColor(0x2E, 0xAB, 0xFB);
            BaseInfo.themeGrayColor = TYZColor(0x6a, 0x72, 0x7a);
            BaseInfo.navigationImage = [UIImage imageWithColor:BaseInfo.themeColor];
            BaseInfo.globalBackgroundColor = ESTBackgroundColor;
            BaseInfo.backButtonImageNameNormal = Image_Navigation_Back_Normal_White;
            BaseInfo.backButtonImageNameHighlight = Image_Navigation_Back_Highlight_White;
            break;
        default:
            BaseInfo.themeColor = TYZColor(0x2E, 0xAB, 0xFB);
            BaseInfo.themeGrayColor = TYZColor(0x6a, 0x72, 0x7a);
            BaseInfo.navigationImage = [UIImage imageWithColor:BaseInfo.themeColor];
            BaseInfo.globalBackgroundColor = ESTBackgroundColor;
            BaseInfo.backButtonImageNameNormal = Image_Navigation_Back_Normal_White;
            BaseInfo.backButtonImageNameHighlight = Image_Navigation_Back_Highlight_White;
            break;
    }
}
/**
 *  获取当前缓存
 */
+ (NSString *)currentCache {
    return @"0.00b";
}
/**
 *  跳转到设置界面
 *
 *  @param pushSettingType 设置界面的类型
 */
+ (void)pushToDeviceSettingWithPushSettingType:(TYZPushSettingType)pushSettingType {
    NSString *urlString = BaseInfo.settingTypeArray[pushSettingType];
    if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:urlString]]) {
        if (@available(iOS 10.0, *)) {
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString] options:@{} completionHandler:nil];
        } else {
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];
        }
    }
}
@end
